package com.example.administrator.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private Bitmap abitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imgv);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
        abitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),bitmap.getConfig());

        final Canvas canvas = new Canvas(abitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);

        Matrix matrix =new Matrix();
        canvas.drawBitmap(bitmap,matrix,paint);
        imageView.setImageBitmap(abitmap);
        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                try {
                    switch (motionEvent.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            Toast.makeText(MainActivity.this,"手指触动",0).show();
                            break;
                        case  MotionEvent.ACTION_MOVE:
                            Toast.makeText(MainActivity.this,"手指移动("+ motionEvent.getX()+","+ motionEvent.getY()+")",0).show();
                            int x = (int) motionEvent.getX();
                            int y = (int) motionEvent.getY();
                            for (int i = -10;i<10;i++){
                                for (int j = -10;j<10;j++) {
                                    if (Math.sqrt((i * i) + (j * j)) < 10) {

                                        abitmap.setPixel(x + i, y + j, Color.TRANSPARENT);
                                    }
                                }
                            }
                            imageView.setImageBitmap(abitmap);
                            break;
                        case MotionEvent.ACTION_UP:
                            Toast.makeText(MainActivity.this,"手指松开",0).show();
                            break;
                    }
                }catch (Exception e){

                    e.printStackTrace();
                }

                return true;
            }
        });


    }
}
